package com.wangjiyuan.giftshuo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.base.App;
import com.wangjiyuan.giftshuo.bean.search.HistoryBean;
import com.wangjiyuan.giftshuo.utils.BaseAdapterHelper;

import java.util.List;

/**
 * Created by wjy on 2016/11/21.
 */

public class HistoryAdapter extends BaseAdapterHelper<HistoryBean> {

    private onListIsEmpty ls;

    public HistoryAdapter(Context context, List list) {
        super(context, list);
    }

    @Override
    public View getItemView(int position, View convertView, ViewGroup parent) {
        viewHolder vh;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_history, parent, false);
            vh = new viewHolder(convertView);


        } else {
            vh = (viewHolder) convertView.getTag();
        }
        vh.Instance(position);
        convertView.setTag(vh);
        return convertView;
    }

    class viewHolder {
        TextView content;
        ImageView delete;
        Long id;

        public viewHolder(View view) {
            content = (TextView) view.findViewById(R.id.content);
            delete = (ImageView) view.findViewById(R.id.delete);

        }

        public void Instance(final int position) {
            id = mList.get(position).getId();
            content.setText(mList.get(position).getContent());
            delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //从数据库中删除此条数据
                    HistoryBean bean = new HistoryBean(id, content.getText().toString());
                    App.app.getDaoSession().delete(bean);
                    mList.remove(position);
                    notifyDataSetChanged();
                    if (ls != null && mList.size() == 0) {
                        ls.onListIsEmpty();
                    }
                }
            });
        }
    }

    public void setListener(onListIsEmpty ls) {
        this.ls = ls;
    }

    public interface onListIsEmpty {
        void onListIsEmpty();
    }
}
